package cn.leida.ruoyi.generator.domain;

import cn.hutool.core.util.StrUtil;
import cn.leida.ruoyi.common.constant.GenConstants;
import cn.leida.ruoyi.common.core.domain.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.util.List;


/**
 * 业务表 gen_table
 *
 * @author liutietou
 * @since 1.0  2020-12-25
 */
@Entity
@Table(name = "gen_table")
@DynamicInsert()
@DynamicUpdate()
@Data
public class GenTable extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 编号
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "table_id")
    private Long tableId;

    /**
     * 表名称
     */
    @NotBlank(message = "表名称不能为空")
    @Column(name = "table_name", length = 200)
    private String tableName;

    /**
     * 表描述
     */
    @NotBlank(message = "表描述不能为空")
    @Column(name = "table_comment", length = 500)
    private String tableComment;

    /**
     * 实体类名称(首字母大写)
     */
    @NotBlank(message = "实体类名称不能为空")
    @Column(name = "class_name", length = 100)
    private String className;

    /**
     * 使用的模板（crud单表操作 tree树表操作）
     */
    @Column(name = "tpl_category", length = 200)
    private String tplCategory;

    /**
     * 生成包路径
     */
    @NotBlank(message = "生成包路径不能为空")
    @Column(name = "package_name", length = 100)
    private String packageName;

    /**
     * 生成模块名
     */
    @NotBlank(message = "生成模块名不能为空")
    @Column(name = "module_name", length = 100)
    private String moduleName;

    /**
     * 生成业务名
     */
    @NotBlank(message = "生成业务名不能为空")
    @Column(name = "business_name", length = 100)
    private String businessName;

    /**
     * 生成功能名
     */
    @NotBlank(message = "生成功能名不能为空")
    @Column(name = "function_name", length = 100)
    private String functionName;

    /**
     * 生成作者
     */
    @NotBlank(message = "作者不能为空")
    @Column(name = "function_author", length = 100)
    private String functionAuthor;

    /**
     * 生成代码方式（0zip压缩包 1自定义路径）
     */
    @Column(name = "gen_type", length = 10)
    private String genType;

    /**
     * 生成路径（不填默认项目路径）
     */
    @Column(name = "gen_path", length = 100)
    private String genPath;

    /**
     * 其它生成选项
     */
    @Column(name = "options", length = 1000)
    private String options;

    @Column(name = "create_by", length = 64)
    private String createBy;

    @Column(name = "create_time", length = 30)
    private String createTime;

    @Column(name = "update_by", length = 64)
    private String updateBy;

    @Column(name = "update_time", length = 30)
    private String updateTime;

    @Column(name = "remark", length = 300)
    private String remark;

    /**
     * 主键信息
     */
    @Transient
    private GenTableColumn pkColumn;

    /**
     * 表列信息
     */
    @Valid
    @Transient
    private List<GenTableColumn> columns;


    /**
     * 树编码字段
     */
    @Transient
    private String treeCode;

    /**
     * 树父编码字段
     */
    @Transient
    private String treeParentCode;
    /**
     * 树名称字段
     */
    @Transient
    private String treeName;
    /**
     * 上级菜单ID字段
     */
    @Transient
    private String parentMenuId;
    /**
     * 上级菜单名称字段
     */
    @Transient
    private String parentMenuName;


    public String moduleNamePath() {
        if(moduleName==null){
            return "";
        }
        return  moduleName.replaceAll("\\.|:", "/");
    }


    public boolean isTree() {
        return isTree(this.tplCategory);
    }

    public static boolean isTree(String tplCategory) {
        return tplCategory != null && StrUtil.equals(GenConstants.TPL_TREE, tplCategory);
    }

    public boolean isCrud() {
        return isCrud(this.tplCategory);
    }

    public static boolean isCrud(String tplCategory) {
        return tplCategory != null && StrUtil.equals(GenConstants.TPL_CRUD, tplCategory);
    }

    public boolean isSuperColumn(String javaField) {
        return isSuperColumn(this.tplCategory, javaField);
    }

    public static boolean isSuperColumn(String tplCategory, String javaField) {
        if (isTree(tplCategory)) {
            return StrUtil.equalsAnyIgnoreCase(javaField,
                ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
        }
        return StrUtil.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
    }


    public void initUpdateTime() {
        updateTime = nowTime();
    }

    public void initCreateTime() {
        createTime = nowTime();
    }




}
